package com.seya.calculite.app;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


public class ProcessActivity extends Activity {

    TextView timer_view_hrs, timer_view_mins, timer_view_secs, kilometer_reading;
    public static ProcessActivity processActivity;
    protected GlobalContext globalContext;
    String totaltime, totalkilometers;
    SharedPreferences prefs;
    LinearLayout gotoMap;
    Button btn_end;
    GPSTracker gps;
    Context c;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        overridePendingTransition(R.anim.activity_open_translate,R.anim.activity_close_scale);
        setContentView(R.layout.activity_process);

        //Enable GPS
        Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
        intent.putExtra("enabled", true);
        sendBroadcast(intent);

        final Intent timerIntent = new Intent(ProcessActivity.this,TimerService.class);

        gps = new GPSTracker(this);

        FontHelper.applyFont(this, findViewById(R.id.activity_main_root), "RobotoLight.ttf");
        FontHelper.applyFont(this, findViewById(R.id.activity_digital), "digital7.ttf");
        FontHelper.applyFont(this, findViewById(R.id.activity_digital_time), "digital7.ttf");

        c = getApplicationContext();
        processActivity = this;

        globalContext = (GlobalContext)this.getApplicationContext();

        timer_view_hrs = (TextView) findViewById(R.id.timing_view_hrs);
        timer_view_mins = (TextView) findViewById(R.id.timing_view_mins);
        timer_view_secs = (TextView) findViewById(R.id.timing_view_secs);
        kilometer_reading = (TextView) findViewById(R.id.km_view);

        gotoMap = (LinearLayout) findViewById(R.id.go_to_map);
        gotoMap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(ProcessActivity.this, MapActivity.class);
                startActivity(i);
            }
        });


        btn_end = (Button)findViewById(R.id.btn_end);
        btn_end.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                stopService(timerIntent);

                totalkilometers = kilometer_reading.getText().toString();
                totaltime = timer_view_hrs.getText().toString() + ":" + timer_view_mins.getText().toString() + ":" +
                        timer_view_secs.getText().toString();

                prefs = c.getSharedPreferences(SplashScreenActivity.SHARED_PREFERENCES_NAME, MODE_PRIVATE);
                SharedPreferences.Editor editor = prefs.edit();
                editor.putString(SplashScreenActivity.KILOMETER, totalkilometers);
                editor.putString(SplashScreenActivity.TOTAL_TIME, totaltime);
                if(gps.canGetLocation()) {
                    editor.putString(SplashScreenActivity.END_LATITUDE, Double.toString(gps.getLatitude()));
                    editor.putString(SplashScreenActivity.END_LONGITUDE, Double.toString(gps.getLongitude()));
                }
                editor.commit();
                Intent i = new Intent(getApplicationContext(),OutputActivity.class);
                startActivity(i);
                finish();

                //Disable GPS
                Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
                intent.putExtra("enabled", false);
                sendBroadcast(intent);
            }
        });
        if(!isMyServiceRunning(TimerService.class)) {
            startService(timerIntent);
        }
    }

    @Override
    protected void onPause()
    {
        super.onPause();
        clearReferences();
        overridePendingTransition(R.anim.activity_open_scale,R.anim.activity_close_translate);
    }

    public boolean isMyServiceRunning(Class<?> serviceClass) {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (serviceClass.getName().equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }

    private void clearReferences(){
        Activity currActivity = globalContext.getCurrentActivity();
        if (currActivity != null && currActivity.equals(this))
            globalContext.setCurrentActivity(null);
    }

    @Override
    protected void onResume() {
        super.onResume();
        globalContext.setCurrentActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        clearReferences();
    }
}
